package com.example.smartcity4;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import com.example.smartcity4.login.LoginActivity;
import com.example.smartcity4.ui.dangjian.DangjianFragment;
import com.example.smartcity4.ui.home.HomeFragment;
import com.example.smartcity4.ui.news.NewsFragment;
import com.example.smartcity4.ui.service.serviceFragment;
import com.example.smartcity4.ui.user.UserFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private HomeFragment fragment1;
    private serviceFragment fragment2;
    private DangjianFragment fragment3;
    private NewsFragment fragment4;
    private UserFragment fragment5;
    private RadioButton home;
    private RadioButton service;
    private RadioButton dangjian;
    private RadioButton news;
    private RadioButton user;
    private List<Fragment> list;
    private FrameLayout fragment;
    public static String host="http://124.93.196.45:10001";
    public static String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       token= getSharedPreferences("SP", MODE_PRIVATE).getString("token", "");
        System.out.println(token);
        if (token.equals("")){
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));

        }
        initView();

    }

    private void initView() {


        fragment = findViewById(R.id.fragment);

        home = findViewById(R.id.home);
        service = findViewById(R.id.service);
        dangjian = findViewById(R.id.dangjian);
        news = findViewById(R.id.news);
        user = findViewById(R.id.user);
        home.setOnClickListener(this);
        service.setOnClickListener(this);
        dangjian.setOnClickListener(this);
        news.setOnClickListener(this);
        user.setOnClickListener(this);
        list = new ArrayList<>();
        fragment1 = new HomeFragment();
        fragment2 = new serviceFragment();
        fragment3 = new DangjianFragment();
        fragment4 = new NewsFragment();
        fragment5 = new UserFragment();
        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        list.add(fragment4);
        list.add(fragment5);
        addFrahment(fragment1);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home:
                addFrahment(fragment1);
                break;
            case R.id.service:
                addFrahment(fragment2);
                break;
            case R.id.dangjian:
                addFrahment(fragment3);
                break;
            case R.id.news:
                addFrahment(fragment4);
                break;
            case R.id.user:
                addFrahment(fragment5);
                break;
        }
    }

    private void addFrahment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment, fragment).commitAllowingStateLoss();

    }
}
